<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">所属农场</label>
        <el-select v-model="query.farm_id" filterable placeholder="请选择" style="width: 185px;" class="filter-item">
          <el-option v-for="item in dict.farms" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <label class="el-form-item-label">田块名称</label>
        <el-input
          v-model="query.field_name"
          clearable
          placeholder="田块名称"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">药品类型</label>
        <el-select v-model="query.spray_type" filterable placeholder="请选择" style="width: 185px;" class="filter-item">
          <el-option v-for="item in dict.spray_type" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="600px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="所属农场" prop="farm_id">
            <el-select
              v-model="form.farm_id"
              filterable
              placeholder="请选择"
              style="width: 370px;"
              @change="changeFram"
            >
              <el-option v-for="item in dict.farms" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="田块" prop="field_id">
            <el-select v-model="form.field_id" filterable placeholder="请选择" style="width: 370px;">
              <el-option v-for="item in fields" :key="item.id" :label="item.field_name" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="药品类型" prop="spray_type">
            <el-select
              v-model="form.spray_type"
              filterable
              placeholder="请选择"
              style="width: 185px;"
              multiple
              @remove-tag="deleteSprayType"
              @change="changeSprayType"
            >
              <el-option v-for="item in dict.spray_type" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="药品名称" prop="spray_name">
            <el-select
              v-model="form.spray_name"
              filterable
              placeholder="请选择"
              style="width: 185px;"
              multiple
              @remove-tag="deleteSprayName"
              @change="changeSprayName"
            >
              <el-option v-for="item in dict.spray_name" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="施药时间">
            <el-date-picker v-model="form.spray_time" type="datetime" style="width: 370px;" value-format="yyyy-MM-dd" />
          </el-form-item>
          <el-form-item label="施药量">
            <el-input v-model="form.yongliang" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="药品厂家">
            <el-input v-model="form.changjia" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="farm_name" label="所属农场" width="150" />
        <el-table-column prop="field_name" label="田块名称" width="200" />
        <el-table-column prop="str_spray_type" label="药品类型" />
        <el-table-column prop="str_spray_name" label="药品名称" />
        <el-table-column prop="spray_time" label="施药时间" width="90" />
        <el-table-column prop="yongliang" label="施药量" />
        <el-table-column prop="changjia" label="药品厂家" width="200" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column
          v-if="checkPer(['admin', 'fieldSpray:edit', 'fieldSpray:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
/** 施药履历  */
import crudFieldSpray from '@/api/jf/fieldSpray'
import { getFieldListByFarm } from '@/api/jf/public'
import CRUD, { crud, form, header, presenter } from '@crud/V2/crud'
import crudOperation from '@crud/V2/CRUD.operation'
import pagination from '@crud/V2/Pagination'
import rrOperation from '@crud/V2/RR.operation'
import udOperation from '@crud/V2/UD.operation'

const defaultForm = {
  id: null,
  farm_id: null,
  field_id: null,
  spray_type: [],
  spray_name: [],
  spray_time: null,
  changjia: null,
  remark: null
}
export default {
  name: 'FieldSpray',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '田块施药履历',
      url: 'api/v2/base/farm/fieldSpray',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudFieldSpray }
    })
  },
  dictsV2: ['farms'],
  dicts: ['spray_type', 'spray_name'],
  data() {
    return {
      permission: {
        add: ['admin', 'fieldSpray:add'],
        edit: ['admin', 'fieldSpray:edit'],
        del: ['admin', 'fieldSpray:del']
      },
      sprayTypeDatas: [], // 药品类型多选
      sprayNameDatas: [], // 药品名称多选
      rules: {
        field_id: [{ required: true, message: '不能为空', trigger: 'blur' }],
        farm_id: [{ required: true, message: '不能为空', trigger: 'blur' }],
        spray_type: [{ required: true, message: '不能为空', trigger: 'blur' }]
      },
      queryTypeOptions: [
        { key: 'farm_id', display_name: '农场编号' },
        { key: 'field_name', display_name: '田块名称' },
        { key: 'spray_type', display_name: '药品类型' }
      ],
      fields: []
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToEdit](crud, form) {
      this.getFields(form.farm_id, true)
      this.sprayTypeDatas = []
      this.sprayNameDatas = []
      const _this = this
      console.log(form)
      form.spray_type.forEach(function(sprayType, index) {
        _this.sprayTypeDatas.push(sprayType)
      })
      form.spray_name.forEach(function(sprayName, index) {
        _this.sprayNameDatas.push(sprayName)
      })
    },
    // 新增前将多选的值设置为空
    [CRUD.HOOK.beforeToAdd]() {
      this.sprayTypeDatas = []
      this.sprayNameDatas = []
    },
    // 提交前做的操作
    [CRUD.HOOK.afterValidateCU](crud) {
      if (this.sprayTypeDatas.length === 0) {
        this.$message({
          message: '药品类型不能为空',
          type: 'warning'
        })
        return false
      }
      if (this.sprayNameDatas.length === 0) {
        this.$message({
          message: '药品名称不能为空',
          type: 'warning'
        })
        return false
      }
      crud.form.spray_type = this.sprayTypeDatas
      crud.form.spray_name = this.sprayNameDatas
      return true
    },
    changeSprayType(value) {
      this.sprayTypeDatas = []
      const _this = this
      value.forEach(function(data, index) {
        const sprayType = data
        _this.sprayTypeDatas.push(sprayType)
      })
    },
    deleteSprayType(value) {
      this.sprayTypeDatas.forEach(function(data, index) {
        if (data === value) {
          this.sprayTypeDatas.splice(index, value)
        }
      })
    },
    changeSprayName(value) {
      this.sprayNameDatas = []
      const _this = this
      value.forEach(function(data, index) {
        const sprayName = data
        _this.sprayNameDatas.push(sprayName)
      })
    },
    deleteSprayName(value) {
      this.sprayNameDatas.forEach(function(data, index) {
        if (data === value) {
          this.sprayNameDatas.splice(index, value)
        }
      })
    },
    // 获取田块
    getFields(farm_id, isNotClear) {
      if (farm_id != null) {
        getFieldListByFarm(farm_id)
          .then(res => {
            this.fields = res.data
          })
          .catch(() => {})
      } else {
        this.fields = []
      }
      if (!isNotClear) {
        this.form.field_id = ''
      }
    },
    changeFram(value) {
      this.getFields(value)
    }
  }
}
</script>

<style scoped></style>
